// 四川农信.Utils.SignUtils
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Text;

namespace CommonUtils
{
	public class SignUtils
	{
		public static string RSASignFromXml(string data, string privateKey)
		{
			byte[] bytesSignCon = Encoding.UTF8.GetBytes(data);
			RSACryptoServiceProvider mchPrivateKey = new RSACryptoServiceProvider();
			mchPrivateKey.FromXmlString(privateKey);
			byte[] bytesHash = mchPrivateKey.SignData(bytesSignCon, "SHA256");
			return Convert.ToBase64String(bytesHash);
		}

		public static string CFCAEncryption(string publicKeyPath, string data)
		{
			X509Certificate2 pubcrt;
			try
			{
				pubcrt = new X509Certificate2(publicKeyPath);
			}
			catch (Exception e)
			{
				throw new Exception(e.Message + " 文件名：" + publicKeyPath);
			}
			string keyPublic2 = pubcrt.PublicKey.Key.ToXmlString(includePrivateParameters: false);
			return RSAEncrypt(keyPublic2, data);
		}

		public static string RSAEncrypt(string publickey, string content)
		{
			RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
			rsa.FromXmlString(publickey);
			byte[] cipherbytes = rsa.Encrypt(Encoding.Default.GetBytes(content), fOAEP: false);
			return Convert.ToBase64String(cipherbytes);
		}

		public static bool SignCheck(string str, string sign, string publicKey)
		{
			try
			{
				byte[] bt = Encoding.GetEncoding("utf-8").GetBytes(str);
				SHA256CryptoServiceProvider sha256 = new SHA256CryptoServiceProvider();
				byte[] rgbHash = sha256.ComputeHash(bt);
				RSACryptoServiceProvider key = new RSACryptoServiceProvider();
				key.FromXmlString(publicKey);
				RSAPKCS1SignatureDeformatter deformatter = new RSAPKCS1SignatureDeformatter(key);
				deformatter.SetHashAlgorithm("SHA256");
				byte[] rgbSignature = Convert.FromBase64String(sign);
				if (deformatter.VerifySignature(rgbHash, rgbSignature))
				{
					return true;
				}
				return false;
			}
			catch (Exception)
			{
				return false;
			}
		}

		public static SortedDictionary<string, string> GetSignDic<T>(T t)
		{
			PropertyInfo[] cfgItemProperties = t.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public);
			SortedDictionary<string, string> sdCfgItem = new SortedDictionary<string, string>();
			PropertyInfo[] array = cfgItemProperties;
			foreach (PropertyInfo item in array)
			{
				string name = item.Name;
				object value = item.GetValue(t, null);
				if (value != null && (item.PropertyType.IsValueType || item.PropertyType.Name.StartsWith("String")) && !string.IsNullOrWhiteSpace(value.ToString()))
				{
					sdCfgItem.Add(name, value.ToString());
				}
			}
			return sdCfgItem;
		}

		public static IDictionary<string, string> AsciiDictionary(IDictionary<string, string> sArray)
		{
			IDictionary<string, string> asciiDic = new Dictionary<string, string>();
			string[] arrKeys = sArray.Keys.ToArray();
			Array.Sort(arrKeys, string.CompareOrdinal);
			string[] array = arrKeys;
			foreach (string key in array)
			{
				string value = sArray[key];
				asciiDic.Add(key, value);
			}
			return asciiDic;
		}

		public static string GetSignContent(IDictionary<string, string> parameters)
		{
			IDictionary<string, string> sortedParams = AsciiDictionary(parameters);
			IEnumerator<KeyValuePair<string, string>> dem = sortedParams.GetEnumerator();
			StringBuilder query = new StringBuilder("");
			while (dem.MoveNext())
			{
				string key = dem.Current.Key;
				string value = dem.Current.Value;
				string val = "";
				if (!string.IsNullOrEmpty(key) && !string.IsNullOrEmpty(value))
				{
					query.Append(key).Append("=").Append(value)
						.Append("&");
				}
			}
			return query.ToString().Substring(0, query.Length - 1);
		}
	}
}